.SUFFIXES:
.PHONY: clean dist install

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET		:= frozen-bubble
BUILD		:= build
SOURCES		:= src
DATA			:= 
INCLUDES		:= 
PLATFORM		:= WII
PLATFORM_LC	:= $(shell echo $(PLATFORM) | tr A-Z a-z)
OUTPUT		:= $(TARGET).$(PLATFORM_LC)
PLOUTPUT		:= $(TARGET).$(PLATFORM)

export DEPSDIR := $(CURDIR)

export DATE := $(shell date +%Y%m%d%H%M%S)

all: $(OUTPUT)

include Makefile.$(PLATFORM_LC)

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------

CFLAGS	+= -g -O2 -Wall -std=gnu99 $(MACHDEP) $(INCLUDE) -DPLATFORM_$(PLATFORM)

LD		= $(CC)
LDFLAGS	= -g $(MACHDEP) -Wl #,-Map,$(notdir $@).map

#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------

CFILES		:=	$(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c))
SFILES		:=	$(foreach dir,$(SOURCES),$(wildcard $(dir)/*.s))
BINFILES		:=	$(foreach dir,$(DATA),$(wildcard $(dir)/*.*))

export OFILES	:=	$(addsuffix .o,$(BINFILES)) \
					$(CFILES:.c=.o) $(SFILES:.s=.o)

export INCLUDE	:=	$(INCLUDE) \
					$(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
					$(foreach dir,$(LIBDIRS),-I$(dir)/include)

export LIBPATHS	:=	$(LIBPATHS) \
					$(foreach dir,$(LIBDIRS),-L$(dir)/lib)

#---------------------------------------------------------------------------------

# TODO: I don't like putting this here
$(TARGET).elf: $(OFILES)

#---------------------------------------------------------------------------------

$(OUTPUT): $(OFILES) $(PLOUTPUT)
	@mv $(PLOUTPUT) $@

clean:
	@echo clean ...
	@rm -rf $(TARGET).* $(OFILES) src/*.d *~ */*~

dist: distdir
	zip -r9 $(TARGET).zip $(TARGET)
	mv $(TARGET).zip $(TARGET)_$(PLATFORM_LC)_r$(shell svn info | grep Revision | cut -d' ' -f2).zip
	rm -rf $(TARGET)

datedist: distdir
	zip -r9 $(TARGET).zip $(TARGET)
	mv $(TARGET).zip $(TARGET)_$(PLATFORM_LC)_${DATE}.zip
	rm -rf $(TARGET)

